const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const mongoose = require('mongoose')
const { connect, initSchemas } = require('./database/init.js')
// let Router_user = require('./appApi/User.js')
let Router_index = require('./appApi/api_index.js')


const bodyParser = require('koa-bodyparser')
const cors = require('koa2-cors') //跨域组件
app.use(cors())//允许跨域
app.use(bodyParser());  //post参数获取-中间件

// 装载路由
let router = new Router();
// router.use('/user',Router_user.routes())
router.use('/index',Router_index.routes())


//加载路由中间件
app.use(router.routes()) /*启动路由*/
app.use(router.allowedMethods())


    //立即执行函数
    ; (async () => {
        
        await connect()
        initSchemas()
        
        // const User = mongoose.model('User')
        // let oneUser = new User({ userName: 'jspang2', password: '123456' })
        // oneUser.save().then(() => {
        //     console.log('插入成功')
        // })
        // let users = await User.findOne({}).exec()
        // console.log('------------------')
        // console.log(users)
        // console.log('------------------')
    })()
// app.use(async (ctx) => {
//     console.log('hello Koa2',ctx.request.body)
//     ctx.body = '<h1>hello Koa2</h1>'
// })
app.listen(3333, () => {
    console.log('[Server] starting at port 3333')
})